import scrapy
from scrapy import FormRequest
from Hue.basepro import ZhengFuBaseSpider


class WuhaiSpider(ZhengFuBaseSpider):
    name = 'Wuhai'
    allowed_domains = ['wuhai.gov.cn']
    start_urls = ['http://www.wuhai.gov.cn/']
    api = "http://www.wuhai.gov.cn/search/pcRender?pageId=63493493b61047b8be9bc396fa236e60"
    keywords = ["碳管理", "碳标识", "低碳认证技术标准", "碳排放控制制度", "低碳供应链制度", "碳达峰追踪制度", "碳合一", "碳积分", "碳评价", "碳金融", "低碳立法支持", "低碳创新机制", "低碳制度创新",]
    method = "POST"
    debug = False
    data = {
	    "originalSearchUrl": "/search/pcRender?pageId=63493493b61047b8be9bc396fa236e60",
	    "originalSearch": "",
        "appName": "",
        "pNo": "",
        "q": ""
    }

    def edit_page(self, response):
        # scrapy.shell.inspect_response(response, self)
        page = response.css("span.default-result-tolal-records > span:nth-child(1)::text").get()
        self.logger.debug(f"总页数: {page}")
        return int(page)

    # DEPRECATED
    # def parse_page(self, response):
    #    """解析总页数"""
    #    page = response.css("#panel-page > div > div.jcyh-lbys > div.default-result-list-paging > span > span::text").get()
    #    return int(page)

    def edit_items_box(self, response):
        items_box = response.css("div.default-result-list > div.searchMod")
        return items_box

    # DEPRECATED
    # def parse_items(self, items, keyword=None, page=None):
    #    items_box = items.css("div.default-result-list > div.searchMod")
    #    return (self.parse_item(item, keyword, page) for item in items_box)

    def edit_item(self, item):
        item_data = {
            "title": ''.join(item.css("div.news-style1 > h3 > a::text, div.news-style1 > h3 > a > font::text").getall()).strip(),
            "url": item.css("p.dates > a::attr(href)").get(),
            "date": item.css("p.dates > span::text").get(),
        }
        return item_data

    # DEPRECATED
    # def parse_item(self, item, keyword, page):
    #    """解析meta信息"""
    #    meta_info = {
    #        "title": ''.join(item.css("div.news-style1 > h3 > a::text, div.news-style1 > h3 > a > font::text").getall()).strip(),
    #        "pre_content": ''.join(item.css("p.txtCon::text, p.txtCon > font::text").getall()).strip(),
    #        "url": item.css("p.dates > a::attr(href)").get(),
    #        "date": item.css("p.dates > span::text").get(),
    #        "keyword": keyword,
    #        "page": page
    #    }
    #    return meta_info

    def edit_data(self, data, keyword, page):
        data["pNo"] = str(page)
        data["q"] = str(keyword)
        return data

    # DEPRECATED
    # def build_form_data(self, page=None, keyword=None, **kwargs):
    #    """构造表单数据"""
    #    form_data = copy(self.data)
    #    if page:
    #        form_data['pNo'] = str(page)
    #    if keyword:
    #        form_data['q'] = keyword
    #    form_data |= kwargs
    #    return form_data

    def edit_article(self, response):
        article = {}
        article["title"] = response.css("div.wh_xl_t::text").get()
        article["content"] = ''.join(response.css("div#wh_x_c *::text").getall())
        article["source"] = response.css("span#ly::text").get()
        return article
